<template>
  <div class="file-upload">
  </div>
</template>

<script>
import CosSDK from 'cos-wx-sdk-v5'
import { genNo } from '../utils/helper'
import * as material from '@/api/material'
import * as cos from '@/api/cos'
import * as _helper from '@/utils/helper'
export default {
  props: {
    path: {
      type: String,
      default: ''
    },
    dir: {
      type: String,
      default: ''
    },
    type: {
      type: String,
      default: 'audio'
    },
    size: {
      type: Number,
      default: 0
    },
    meta: {
      type: Object,
      default() {
        return null
      }
    },
    busket: {
      type: String,
      default: 'ark-1256750454'
    },
    contentType: {
      type: String,
      default: 'audio/mpeg'
    },
    region: {
      type: String,
      default: 'ap-shanghai'
    }
  },
  data() {
    return {
      cos: null,
      key: '',
      path: '',
      isUploading: false,
      status: 'upload-unstart',
      cosAuth: {
        sessionToken: '',
        tmpSecretId: '',
        tmpSecretKey: '',
        expiredTime: ''
      }
    }
  },
  computed: {},
  async mounted() {
    this.cosAuth = await cos.getAuth()
  },
  methods: {
    upload () {
      if (!this.path || !this.cosAuth.sessionToken || !this.cos) {
        this.status = 'upload-error'
        return
      }
      this.$emit('start')
      this.isUploading = true
      let that = this
      let dir = _helper.rtrim(this.dir, '/')
      this.key = dir ? `${this.dir}/${genNo()}` : genNo()
      that.status = 'upload-ing'
      this.cos.postObject(
        {
          Bucket: this.busket,
          Region: this.region,
          Key: this.key,
          FilePath: this.path,
          ContentType: that.contentType,
          onProgress: function(info) {
            that.$emit('progress', info)
          }
        },
        function(err, data) {
          that.$emit('back', {err, data})
          if (err) return
          if (data.statusCode !== 200) {
            that.status = 'upload-fail'
            return
          }
          that.status = 'upload-success'
        }
      )
    }
  },
  watch: {
    path (nv) {
      if (!nv) {
        this.status = 'upload-error'
        return
      }
      this.upload()
    },
    cos (nv) {
      this.upload()
    },
    'cosAuth.sessionToken'(nv) {
      if (!nv) return
      let that = this
      this.cos = new CosSDK({
        /* eslint-disable */
        getAuthorization: function (options, callback) {
          callback({
            TmpSecretId: that.cosAuth.tmpSecretId,
            TmpSecretKey: that.cosAuth.tmpSecretKey,
            XCosSecurityToken: that.cosAuth.sessionToken,
            ExpiredTime: that.cosAuth.expiredTime
          })
        }
        /* eslint-enable */
      })
    },
    async status(nv) {
      if (nv !== 'upload-success') return
      let result = await material.createMaterial({
        type: this.type,
        key: this.key,
        size: this.size,
        meta: this.meta
      })
      this.isUploading = false
      this.$emit('success', result)
    }
  }
}
</script>

<style scoped lang="less">
@import '../assets/less/var.less';
</style>
